package com.atguigu.xuexiplus.config;

import org.springframework.stereotype.Component;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求日志过滤器
 */
@Component
public class RequestLoggingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        
        String method = httpRequest.getMethod();
        String uri = httpRequest.getRequestURI();
        String queryString = httpRequest.getQueryString();
        
        System.out.println("=== 收到请求 ===");
        System.out.println("方法: " + method);
        System.out.println("URI: " + uri);
        System.out.println("查询参数: " + queryString);
        System.out.println("来源: " + httpRequest.getRemoteAddr());
        System.out.println("==================");
        
        // 继续处理请求
        chain.doFilter(request, response);
        
        System.out.println("=== 响应完成 ===");
        System.out.println("状态码: " + httpResponse.getStatus());
        System.out.println("==================");
    }
}
